<?php

$_SESSION['pvwebdebug']=(!isset($_GET['debug']))?($_SESSION['pvwebdebug']):($_GET['debug']=="");
$pvwebdebug=$_SESSION['pvwebdebug'];

//if ($pvwebdebug) echo "start: ".date("d-m-Y H:i.s")."<br>";

include_once "wpvfunctions.php";
wpvconnect();

function insertflukso($debug, $timestamp, $sensorid, $reading) { 
 $sql="insert into wum_raw (entry, sensorid, reading) values (from_unixtime($timestamp), $sensorid, $reading)";
 mysql_query($sql);
 return mysql_affected_rows();
};

// get sensordetails from db
$sql="select sensorid, descr, sensor, token from wum_sensors where sensorid>0";
$result=mysql_query($sql);
$sensorcount=0;
while ($row=mysql_fetch_array($result)) {
 $flsensors[$row[0]]=$row;
 $sensorcount++;
};
$flsensors[0][0]=$sensorcount; // store # sensors in array
mysql_free_result($result);

// get last timestamp for each sensor
$getsensor=1;
do {
 $sql="select unix_timestamp(entry),reading from wum_raw where sensorid=".$getsensor." order by entry desc limit 1";
 $result=mysql_query($sql);
 $row=mysql_fetch_array($result);
 //echo "sensor".$getsensor."=".$row[0]." / ".$row[1]."<br>";
 $lastsensorentry[$getsensor][entry]=$row[0];
 $lastsensorentry[$getsensor][reading]=$row[1];
 //echo "sensor".$getsensor."=".$lastsensorentry[$getsensor][entry]." / ".$lastsensorentry[$getsensor][reading]."<hr>";
 $getsensor++;
} while ($getsensor<=$sensorcount);

$dataerrorcounter=0;
$dataduplicatecounter=0;
$datanullcounter=0;
$dataokcounter=0;
$getsensor=1;
// loopke voor elke sensor
do {
 $FluksoCurl = curl_init();
 $cursensor=$flsensors[$getsensor][2];
 $cursensortoken=$flsensors[$getsensor][3];
 if ($pvwebdebug) echo "-- sensor".$getsensor." (".$flsensors[$getsensor][1].") previous: ".date("j-n-y H:i",$lastsensorentry[$getsensor][entry]); //." / ".$lastsensorentry[$getsensor][reading];
 $rurl="https://api.flukso.net/sensor/".$cursensor."?version=1.0&token=".$cursensortoken."&interval=day&resolution=minute&unit=watt";
 curl_setopt($FluksoCurl, CURLOPT_URL, $rurl);
 curl_setopt($FluksoCurl, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($FluksoCurl, CURLOPT_SSL_VERIFYPEER, false);
 $FluksoResponse=curl_exec($FluksoCurl);  // get data from Fluksoserver
 curl_close($FluksoCurl);
 $FluksoResponseArray=json_decode($FluksoResponse, true);
 //if ($pvwebdebug) echo "number of values:".(count($FluksoResponseArray))."<br>";

 // insert all values
 foreach ($FluksoResponseArray as $ts) {
  if ($ts[1]<>"nan") {
   if ($ts[0]>$lastsensorentry[$getsensor][entry]) {
    //if ($pvwebdebug) echo "importing timestamp ".date("d-m-Y H:i.s",$ts[0]).", data=".$ts[1]."<br>";
    $sqlinserted=insertflukso($pvwebdebug,$ts[0],$getsensor,$ts[1]);
    if ($sqlinserted!=1) $dataerrorcounter++; else $dataokcounter++;
   } else $dataduplicatecounter++;
  } else $datanullcounter++;
 };
 if ($pvwebdebug) {
  $sql="select unix_timestamp(entry), reading from wum_raw where sensorid=".$getsensor." order by entry desc limit 1";
  $result=mysql_query($sql);
  $row=mysql_fetch_array($result);
  echo " -- current: ".date("j-n-y H:i",$row[0])." (".$row[1]."W) ";
  echo "imported ".sprintf("%d",($row[0]-$lastsensorentry[$getsensor][entry])/3600).":".sprintf("%d",($row[0]-$lastsensorentry[$getsensor][entry])%3600/60)." h<br>";
 }
 $getsensor++;
} while ($getsensor<=$sensorcount);

if ($dataokcounter>0 or $dataerrorcounter>0) {
 writelog("fluksoimport:".$dataokcounter,"duplicate:".$dataduplicatecounter."/null:".$datanullcounter,"error:".$dataerrorcounter);
}; // else if ($pvwebdebug) echo "-- nothing imported<br>";
if ($pvwebdebug) {
 echo "-- # entries imported: $dataokcounter<br>";
 if ($datanullcounter>0) echo "-- # null entries skipped: $datanullcounter<br>";
 if ($dataduplicatecounter>0) echo "-- # duplicate entries skipped: $dataduplicatecounter<br>";
 if ($dataerrorcounter>0) echo "-- # entries skipped due to import error: $dataerrorcounter<br>";
};

//if ($pvwebdebug) echo "stop: ".date("d-m-Y H:i.s")."<br>";
?>